package com.lebaduc.itemAttribute;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.security.KeyStore.Entry;
import java.util.Iterator;
import java.util.TreeMap;

public class ExtractItemAttribute {

	static void WritetoFile(int arr[], String filename) {

		try {
			// delete file if exists
			File f = new File("C:\\Users\\sev_user\\Desktop\\output matrix\\"
					+ filename);
			if (f.exists()) {
				f.delete();
			}

			FileWriter fw = new FileWriter(
					"C:\\Users\\sev_user\\Desktop\\output matrix\\" + filename,
					true);
			@SuppressWarnings("resource")
			BufferedWriter bw = new BufferedWriter(fw);
			for (int i = 0; i < arr.length; i++) {
				bw.write(arr[i] + " ");
			}
			bw.newLine();
			bw.flush();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	static void resetYearArray(int years[]) {
		for (int i = 0; i < years.length; i++) {
			years[i] = 0;
		}
	}

	static void resetGenre(int genne[]){
		for (int i = 0; i < genne.length; i++) {
			genne[i]= 0 ;
		}
	}
	public static void main(String[] args) {

		String[] columns = null;
		String[] temp = null;
		String line = null;
		int count = 0;
		int count1 = 0;
		int[] genre = new int[19];
		int[] years = new int[72];
		FileReader fr;
		TreeMap<String, Integer> temp_map_years = new TreeMap<String, Integer>();
		TreeMap<String, Integer> map_years = new TreeMap<String, Integer>();

		try {
			fr = new FileReader("C:\\Users\\sev_user\\Desktop\\ml-100k\\u.item");
			@SuppressWarnings("resource")
			BufferedReader br = new BufferedReader(fr);
			while ((line = br.readLine()) != null) {
				columns = line.split("\\|");

				// == Tao ma tran genre cua item
                 resetGenre(genre);
				for (int i = 5; i <= 23; i++) {
					genre[i - 5] = Integer.parseInt(columns[i]);
				}
				WritetoFile(genre, "i.genre");

				// Create tree map year to calculate how many year we have  ;
				temp = columns[1].split(" ");
				temp_map_years.put(temp[temp.length - 1], count++);

			}
			br.close();
			//========== Create TreeMap year from tem_map_year  with value is number that ascending 
			for (java.util.Map.Entry<String, Integer> child : temp_map_years
					.entrySet()) {
				map_years.put(child.getKey(), count1++);
			}

		} catch (Exception e) {

			e.printStackTrace();
		}
		
		//Write item date to years file 
		
		try {
			fr = new FileReader("C:\\Users\\sev_user\\Desktop\\ml-100k\\u.item");
			@SuppressWarnings("resource")
			BufferedReader br = new BufferedReader(fr);
			while ((line = br.readLine()) != null) {

				columns = line.split("\\|");
				temp = columns[1].split(" ");
				String year = temp[temp.length - 1];
				resetYearArray(years);
				for (java.util.Map.Entry<String, Integer> child : map_years
						.entrySet()) {
					if (year.equalsIgnoreCase(child.getKey())) {
						years[child.getValue()] = 1;
						break;
					}
				}
				WritetoFile(years, "i.date");
			}
			
			br.close() ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}

	}

}
